package com.tssup.fesco.common.oss.object;

import java.util.ArrayList;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.tssup.fesco.common.oss.AliOssClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 删除对象
 *
 * @author shuchang.liao
 * @create 2017-05-15 11:14
 */
@Component
public class DeleteObject
{

	@Autowired
	private AliOssClientFactory aliOssClientFactory;
	/**
	 * 删除对象
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param key
	 *            要删除的对象id
	 * @return 返回删除结果<br/>
	 *         true=删除成功<br/>
	 *         false=删除失败
	 */
	public boolean deleteObject(String bucketName, String key)
	{
		OSSClient client = aliOssClientFactory.getClient();

		try
		{
			client.deleteObject(bucketName, key);

			return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 删除对象列表
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param keyList
	 *            要删除的对象id列表
	 * @return 返回已删除的对象id列表，删除失败时返回null
	 */
	public ArrayList<String> deleteObjects(String bucketName, ArrayList<String> keyList)
	{
		OSSClient client = aliOssClientFactory.getClient();

		DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName).withKeys(keyList);

		try
		{
			DeleteObjectsResult result = client.deleteObjects(request);

			return (ArrayList<String>) result.getDeletedObjects();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
}
